<template>
	<view class="goods-item">
		  <!-- 左侧的数据 -->
		  <view class="goods-item-left">
        <radio :checked="item.goods_state" color="#C00000" v-if="showRadio" @click="radioClickHandler"></radio>
		    <image :src="item.goods_small_logo || defaultPic"></image>
		  </view>
		  <!-- 右侧的数据 -->
		  <view class="goods-item-right">
		    <!-- 商品信息 -->
		    <view class="goods-right-info">{{item.goods_name}}</view>
		    <!-- 价格 -->
		    <view class="goods-right-price">
          <view class="goods-price">
            ￥{{item.goods_price | toFixed}}
          </view>
          <uni-number-box :min="1" @change="numChangeHandler" v-if="showNum" :value="item.goods_count"></uni-number-box>
        </view>
		  </view>
	</view>
</template>

<script>
	export default {
    // 定义props属性，用来接收外界传递到组件的数据
    props: {
      // 商品的信息对象
     item: {
        type: Object,
        default: {},
      },
      // 是否展示图片左侧的radio
      showRadio: {
        type: Boolean,
        // 默认不展示
        default: false
      },
      // 默认不显示数字框
      showNum: {
        type: Boolean,
        default: false
      }
    },
		data() {
			return {
				// 图示失效时默认显示的图片
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
			};
		},
    // 使用过滤器处理价格
    filters: {
      // 把数字处理为带两位小数点的数字
      toFixed (num) {
        return Number(num).toFixed(2)
      }
    },
    methods: {
      // 点击radio的事件处理函数
      radioClickHandler () {
        // 通过this.$emit方法触发外界绑定的radio-change事件
        this.$emit('radio-change', {
          // 商品的id和商品的勾选转态传递给radio-change事件
          goods_id: this.item.goods_id,
          goods_state: !this.item.goods_state
        })
      },
      // 数字选择框的change事件
      numChangeHandler (val) {
        // 通过this.$emit方法触发外界的num-change事件，同时将商品的id和最新的商品数量也传递过去
        this.$emit('num-change', {
          goods_id: this.item.goods_id,
          goods_count: +val
        })
      }
    }
	}
</script>

<style lang="scss">
  .goods-item{
    padding: 10px 5px;
    display: flex;
    border-bottom: 1px solid #efefef;
    .goods-item-left{
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-right: 5px;
      image{
        width: 100px;
        height: 100px;
      }
    }
  }
  .goods-item-right{
   display: flex;
   flex: 1;
   flex-direction: column;
   justify-content: space-between;
    .goods-right-info{
      font-size: 13px;
    }
    .goods-right-price{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .goods-price{
        color: #C00000;
        font-size: 16px;
      }
    }
  }
</style>
